<?php

class UsersController extends Controller
{

  public function actionList()
  {
      $users = User::model()->findAll();
      $this->render('list', array('users' => $users));
  }

  public function actionSignin()
  {
    $model = new LoginForm;
    if (Yii::app()->request->isPostRequest) {
      $userData = Yii::app()->request->getPost('user', false);

      $model->attributes=$userData;

      // validate user input and redirect to the previous page if valid
      if($model->validate() && $model->login()) {
        $this->redirect('/');
      } else {
        $this->render('signin', array('form' => $model));
      }
    } else {
      $this->render('signin', array('form' => $model));
    }
  }

  public function actionAdd()
  {
    if (Yii::app()->user->role == 'admin'){
      if (Yii::app()->request->isPostRequest) {
        $userData = Yii::app()->request->getPost('data', false);
        
        $model = new User();
        $model->username = $userData['username'];
        $model->password = $model->hashPassword($userData['password']);
        $model->phone = $userData['phone'];
		$model->fname = $userData['fname'];
        $model->lname = $userData['lname'];
        $model->email = $userData['email'];
        $model->role = $userData['role'];
        
        $photos = Yii::app()->request->getPost('photos', false);
            if ($photos['outside'] != ''){
                $filename = $photos['outside']; 
                $uploadDir = '/public/photos/';
                $tempFile  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . 'tmp/' . $filename;
                $targetFile = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . $filename;
                rename($tempFile, $targetFile);
                $model->image = $photos['outside'];
            }
        $model->save();
        $this->redirect('/users/profile/'.$model->id);
            
        $this->render('add', array('success' => true));
      } else {
        $this->render('add', array());
      }
    }else{
      $this->redirect('/users/list/');
    }
  }

  public function actionEdit($id)
  {
    $user = User::model()->findByPk($id);
    if ($user && Yii::app()->user->role == 'admin'){
        
          if ((isset($_POST['action'])) && ($_POST['action'] == 'edit')) {
            $userData = Yii::app()->request->getPost('data', false);
            //var_dump($userData); exit;
            $user->username = $userData['username'];
            $user->fname = $userData['fname'];
            $user->lname = $userData['lname'];
            $user->email = $userData['email'];
    		$user->phone = $userData['phone'];
            $user->role = $userData['role'];
            
            $photos = Yii::app()->request->getPost('photos', false);
            if ($photos['outside'] != ''){
                $filename = $photos['outside']; 
                $uploadDir = '/public/photos/';
                $tempFile  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . 'tmp/' . $filename;
                $targetFile = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . $filename;
                rename($tempFile, $targetFile);
                $user->image = $photos['outside'];
            }
            $user->update();
            $this->redirect('/users/profile/'.$user->id); 
          } else{
            $this->render('edit', array('user' => $user));
          } 
            
    }else{
      $this->redirect('/users/list');
    }
  }

  public function actionDelete($id)
  {
    $user = User::model()->findByPk($id);
    if (Yii::app()->user->role == 'admin' && $user){
      if (Yii::app()->request->isPostRequest){
        $user->delete();
        $this->render('delete', array('success' => true));
      }else{
        $this->render('delete', array('user' => $user));
      }
    }else{
      $this->redirect('/users/list');
    }
  }

  public function actionAdmin()
  {
    $id = Yii::app()->request->getPost('id', false);
    $user = User::model()->findByPk($id);
    if (Yii::app()->user->role == 'admin' && $user && Yii::app()->request->isPostRequest){
        $user->role = 'admin';
        $user->update();
        echo CJSON::encode(array('result' => true));
    }else{
      echo CJSON::encode(array('result' => false));
    }
  }

  public function actionSignout()
  {
    Yii::app()->user->logout();
    $this->redirect('/');
  }
    
  public function actionProfile($id){
    if($id){
        $user = User::model()->findByPk($id);
        if($user){
            $this->render('profile', array('success' => true, 'user'=>$user)); 
        }else{
            $this->redirect('/users/list');
        }
    }else{
         $this->redirect('/users/list');
    }
  }
  
  public function actionDetail($id){
    if($id){
        $user = User::model()->findByPk($id);
        if($user){
            echo "kaka";
        }else{
            $this->redirect('/users/list');
        }
    }else{
         $this->redirect('/users/list');
    }
  }
  
  private function _photo_upload($type, $filename, $did, $log_date, $clid)
  {
    $uploadDir = '/public/photos/';
    $tempFile  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . 'tmp/' . $filename;
    $targetFile = $_SERVER['DOCUMENT_ROOT'] . $uploadDir . $filename;
    rename($tempFile, $targetFile);
    
    $photo = new Photos();
    $photo->distributor_id = $did;
    $photo->name = $filename;
    $photo->photo_type = $type;
    $photo->upload_date = date("Y-m-d H:i:s");
    $photo->show_photo = 1;
    $photo->visit_date = $log_date;
    $photo->user_id = Yii::app()->user->id;
    $photo->contact_log_id = $clid;
    $photo->save();
    return $photo->id;
  }
  
  
}